goods = {
		enterNumber : function($o){
			$o.css({'background':'#990099', 'color':'#fff', 'cursor':'pointer'});
		},
		leaveNumber : function($o){
			$o.removeAttr('style');
		},
		showBtn : function($o){
			$o.find('.sub-goods-btn a').css('display', 'block');
		},
		hideBtn : function($o){
			$o.find('.sub-goods-btn a').removeAttr('style');
		},
		getSubGoods : function($o){
			data = 'mid=' + $o.prev('.check').find('input[type=checkbox]').val();
			url = site.url + 'goods/getSubGoods/';
			status = 'GET_SUCCESS';
			$('.sub-goods-list').removeClass('sub-goods-display').slideUp(300);
			main.doAjax(url, data, status, function(r){
				//here we go
				html = '<div class="sub-goods-list">';
				if (r.d.length > 0){
					for (i=0; i<r.d.length; i++){
						html = html + '<div class="each-sub-goods del-flg-' + r.d[i].del_flg + '">';
						html = html + '<div class="check"><input type="hidden" name="sub_goods_id[]" value="' + r.d[i].sub_goods_id + '" /></div>';
						html = html + '<div class="sub-goods-attr">' + r.d[i].attribute + '</div>';/***
						html = html + '<div class="sub-goods-number">' + r.d[i].number + '</div>';
						***/
						if (r.d[i].del_flg == 0) {
							html = html + '<div class="sub-goods-btn"><a href="' + site.url + 'goods/update/sub/' + r.d[i].sub_goods_id + '" target="_self" class="edit"></a><a class="del" href="javascript:void(0)"></a></div>';
						}
						else {
							html = html + '<div class="sub-goods-btn"><a href="' + site.url + 'goods/update/sub/' + r.d[i].sub_goods_id + '" target="_self" class="edit"></a><a class="recover" href="javascript:void(0)"></a></div>';
						}
						html = html + '<div class="sub-goods-stock"><span class="c-009F52">主：' + r.d[i].stock + '</span> [副：<span class="c-red">' + r.d[i].stock_unkown + '</span>]' + ' 件</div>';	
						//html = html + '<div class="sub-goods-code">' + r.d[i].search_code + '</div>';
						html = html + '<div class="sub-goods-update">' + tool.getLocalTime(r.d[i].update_date) + '</div>';
						html = html + '</div>';
					}
				}
				else{
					html = html + '<div class="each-sub-goods"><div class="sub-goods-number">暂无子货品</div></div>';
				}
				html = html + '</div>'
				$html = $(html).addClass('hide');
				$o.parent('.each').after($html);
				$o.parent('.each').css('background', '#eee').next('.sub-goods-list').addClass('sub-goods-display').slideDown(300);
				//绑定新的事件
				$o.unbind('click').bind('click', function(){goods.alreadyGet($o)});
				$o.parent('.each').next('.sub-goods-list').find('.each-sub-goods')
				.bind('mouseenter', function(){goods.showBtn($(this))})
				.bind('mouseleave', function(){goods.hideBtn($(this))});
				$o.parent('.each').next('.sub-goods-list').find('.del').bind('click', function(){goods.ajaxSubGoods($(this))});
				$o.parent('.each').next('.sub-goods-list').find('.recover').bind('click', function(){goods.ajaxSubGoods($(this))});
			});
		},
		//如果已经获得了子货品
		alreadyGet : function($o){
			$display = $o.parent('.each').next('.sub-goods-display');
			if ($display.length == 0){
				$('.sub-goods-display').slideUp(300).removeClass('sub-goods-display');
				$o.parent('.each').next('.sub-goods-list').slideDown(300).addClass('sub-goods-display');
			}
		},
		initSupplier : function(){
			if ($('#supplier-list').children('a').length == 0){
				$('#supplier-list').append('<div class="supplier">无可用项</div>');
			};
			if ($('#suppliers-info').children('.supplier-info').length == 0){
				$('#suppliers-info').append('<div class="supplier-info" style="text-indent:0.5em;color:#333;">暂无关联供应商</div>')
			}
		},
		//编辑子货品
		addSupplier : function($o){
			$o.appendTo('#supplier-added').removeClass('supplier').addClass('group-add-each');
			//重新绑定事件
			$o.bind('click', function(){goods.removeSupplier($(this))});
			//赋值给INPUT
			goods.setSupplier();
		},
		removeSupplier : function($o){
			//不能删除的值
			if ($o.attr('added') == 'true') return false;
			$o.appendTo('#supplier-list').addClass('supplier').removeClass('group-add-each');
			//重新绑定事件
			$o.bind('click', function(){goods.addSupplier($(this))});
			//赋值给INPUT
			goods.setSupplier();
		},
		setSupplier : function(){
			value = '';
			$('.group-add-each').each(function(){
				value = value + $(this).attr('value') + '|';
			})
			if ('' != value) value = value.slice(0, -1);
			$('input[name="supplier_ids"]').val(value);
		},
		ajaxSubGoods : function($o){
			$obj = $o.parents('.each-sub-goods');
			$tobj = $o.parent().siblings('.check').children('input');
			if ('del' == $o.attr('class')){
				url = site.url + 'goods/subGoods/del/';
				ajax.dataList.del(url, $tobj, 'sub_goods_id', function(){
					$obj.removeClass('del-flg-0').addClass('del-flg-1').find('.del').addClass('recover').removeClass('del');
				});
			}
			else if ('recover' == $o.attr('class')){
				url = site.url + 'goods/subGoods/recover/';
				ajax.dataList.recover(url, $tobj, 'sub_goods_id', function(){
					$obj.removeClass('del-flg-1').addClass('del-flg-0').find('.recover').addClass('del').removeClass('recover');
				});
			}
		},
		ajaxMainGoods : function($o){
			if ('del' == $o.attr('class') || 'check-del' == $o.attr('id')){
				if ($o.attr('id') == 'check-del') {
					$tobj = $('.each input[type=checkbox]:checked');
					$obj = $('input[type=checkbox]:checked').parents('.each');
				}
				else if ($o.attr('class') == 'del') {
					$tobj = $o.parents('.each').find('input[type=checkbox]');
					$obj = $o.parents('.each');
				}
				else {
					return false;
				}
				url = site.url + 'goods/mainGoods/del/';
				ajax.dataList.del(url, $tobj, 'main_goods_id', function(){
					$obj.next('.sub-goods-list').slideUp(200, function(){$(this).remove()});
					$obj.slideUp(400, function(){$(this).remove()});
				});
			}
			else if ('recover' == $o.attr('class') || 'check-recover' == $o.attr('id')){
				if ($o.attr('id') == 'check-recover') {
					$tobj = $('.each input[type=checkbox]:checked');
					$obj = $('input[type=checkbox]:checked').parents('.each');
				}
				else if ($o.attr('class') == 'recover') {
					$tobj = $o.parents('.each').find('input[type=checkbox]');
					$obj = $o.parents('.each');
				}
				else {
					return false;
				}
				url = site.url + 'goods/mainGoods/recover/';
				ajax.dataList.recover(url, $tobj, 'main_goods_id', function(){
					$obj.next('.sub-goods-list').slideUp(200, function(){$(this).remove()});
					$obj.slideUp(400, function(){$(this).remove()});
				});
			}
			else {
				return false;
			}
		},
		ajaxSearch : function(){

		}
	}

$(function(){
	$('.each .number')
	.bind('mouseenter', function(){goods.enterNumber($(this))})
	.bind('mouseleave', function(){goods.leaveNumber($(this))})
	.bind('click', function(){goods.getSubGoods($(this))});

	//绑定主货品按钮方法
	$('.each .del').bind('click', function(){goods.ajaxMainGoods($(this))});
	$('#check-del').bind('click', function(){goods.ajaxMainGoods($(this))});
	$('.each .recover').bind('click', function(){goods.ajaxMainGoods($(this))});
	$('#check-recover').bind('click', function(){goods.ajaxMainGoods($(this))});

	//绑定子货品按钮方法
	$('.each-sub-goods .del').unbind('click').bind('click', function(){goods.ajaxSubGoods($(this))});
	$('.each-sub-goods .recover').unbind('click').bind('click', function(){goods.ajaxSubGoods($(this))});

	//绑定搜索
	$('#search a').bind('click', function(){dataList.search()});
	$('#main-goods').bind('submit', function(){dataList.search()});
})